import { SPlayerCountryInfo } from "../../../../Message/Game/SPlayerCountryInfo";
import { ObjCreate } from "../common/ObjCreate";
import { GlobalStyle } from "../../../common/global/GlobalStyle";
import { GTextFormat } from "../../../common/GTextFormat";
import { SWFPlayer } from "../../scene/modle/SWFPlayer";
import { CountryPivillageOperPanel } from "./CountryPivillageOperPanel";
import { CountryConfig } from "../../resource/CountryConfig";
import { ToolTipItem } from "../../../../com/mui/manager/ToolTipItem";
import { Cache } from "../../cache/Cache";
import { ResFileConst } from "../../resource/ResFileConst";
import { ECountryPosition } from "../../../../Message/Game/ECountryPosition";
import { ECountryPrivilege } from "../../../../Message/Game/ECountryPrivilege";
import { SOfficerInfo } from "../../../../Message/Game/SOfficerInfo";
import { Language } from "../../../../extend/language/Language";
import { ESex } from "../../../../Message/Public/ESex";
import { ModelType } from "../../scene/modle/data/ModelType";
import { LoaderHelp } from "../../../common/display/LoaderHelp";
import { GlobalClass } from "../../../../com/mui/core/GlobalClass";
import { ImagesConst } from "../../resource/ImagesConst";
import { GTextFiled } from "../../../../com/mui/controls/GTextFiled";
import { GLoadedButton } from "../../../../com/mui/controls/GLoadedButton";
import { TCountryPrivilege } from "../../../../Message/Db/Tables/TCountryPrivilege";
type int = number;
//class CountryPositionItem
    
    export  class CountryPositionItem /* flash.display.Sprite */
    {
        constructor(arg1: int, arg2: SPlayerCountryInfo, arg3: string="")
        {
            
            this._privilegeArr = [];
            this._position = arg1;
            this._selfCountryInfo = arg2;
            this._oper = arg3;
            if (arg2) 
            {
                this._selfPosition = arg2.position;
            }
            this.init();
            return;
        }

        public get position(): int
        {
            return this._position;
        }

        /* internal  */init(): void
        {
            this._playerNameBg = new Object /* flash.display.Bitmap */();
            this._playerNameBg.x = -20;
            this._playerNameBg.y = 25;
            addChild(this._playerNameBg);
            this._posName = ObjCreate.createGTextField("", 0, 0, 150, 30, null, GlobalStyle.getTextFormatCenter(GlobalStyle.textFormat6));
            this._playerName = ObjCreate.createGTextField("", -35, 25, 115, 20, this, GlobalStyle.getTextFormatCenter(new GTextFormat("", 12, 16776960)));
            this._playerName.addEventListener(flash.events.TextEvent.LINK, this.onPlayerNameLinkHandler);
            this._posNameBmp = new Object /* flash.display.Bitmap */();
            this._modelPlayer = new SWFPlayer();
            this._modelPlayer.loadComplete = this.onModelLoadComplete;
            this._privillageOper = new CountryPivillageOperPanel();
            this._privillageOper.position = this._position;
            this._info = CountryConfig.instance.getCountryPrivilegeInfo(this._position);
            var loc1=new ToolTipItem();
            loc1.addChild(this._posNameBmp);
            addChild(loc1);
            if (this._info) 
            {
                loc1.toolTipData = this._info.positionDes;
            }
            return;
        }

        /* internal  */onPlayerNameLinkHandler(arg1: Object /* flash.events.TextEvent */): void
        {
            return;
        }

        /* internal  */updatePositionInfo(): void
        {
            var loc2=null;
            var loc3=0;
            this._privillageOper.removeOperBtn();
            this._selfCountryInfo = Cache.instance.country.selfCountryInfo;
            if (this._selfCountryInfo) 
            {
                this._selfPosition = this._selfCountryInfo.position;
            }
            var loc1=CountryConfig.instance.getCountryPrivilegeInfo(this._selfPosition);
            if (loc1) 
            {
                this._privilegeArr = loc1.privilege.split("#");
                loc2 = "";
                if (this._privilegeArr && this._privilegeArr.length) 
                {
                    loc3 = 0;
                    while (loc3 < this._privilegeArr.length) 
                    {
                        loc2 = this._privilegeArr[loc3];
                        this.createPrivilege(loc2);
                        ++loc3;
                    }
                }
                if (loc1.canResign && this._position == this._selfPosition) 
                {
                    this._privillageOper.addPrivilegeOper("resign");
                }
            }
            if (this._privillageOper.privillageNum > 0) 
            {
                if (this._operBtn == null) 
                {
                    this._operBtn = ObjCreate.createGLoadedButton(ResFileConst.CountryPosOperBtn, 0, 190, 42, 21, this);
                    this._operBtn.addEventListener(flash.events.MouseEvent.CLICK, this.onOperBtnClickHandler);
                    if (this._oper != "left") 
                    {
                        if (this._oper == "right") 
                        {
                            this._operBtn.x = -70;
                        }
                    }
                    else if (this._position != ECountryPosition._ECountryPositionKing) 
                    {
                        this._operBtn.x = 50;
                    }
                    else 
                    {
                        this._operBtn.x = 10;
                        this._operBtn.y = 210;
                    }
                }
                addChild(this._operBtn);
            }
            else if (this._operBtn && this._operBtn.parent) 
            {
                removeChild(this._operBtn);
            }
            if (this._privillageOper && this._privillageOper.parent) 
            {
                removeChild(this._privillageOper);
            }
            return;
        }

        /* internal  */onOperBtnClickHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            if (this._privillageOper && !this._privillageOper.parent) 
            {
                if (this._oper != "left") 
                {
                    if (this._oper == "right") 
                    {
                        this._privillageOper.x = this._operBtn.x - 80;
                    }
                }
                else 
                {
                    this._privillageOper.x = this._operBtn.x + this._operBtn.width;
                }
                this._privillageOper.y = this._operBtn.y;
                addChild(this._privillageOper);
            }
            else 
            {
                removeChild(this._privillageOper);
            }
            return;
        }

        /* internal  */createPrivilege(arg1: string): void
        {
            if (arg1 == null || arg1 == "") 
            {
                return;
            }
            var loc1=[];
            loc1 = arg1.split(",");
            var loc2=1;
            if (loc1[0] != ECountryPrivilege._ECountryPrivilegePositionOper) 
            {
                if (this._position == this._selfPosition) 
                {
                    if (loc1[0] != ECountryPrivilege._ECountryPrivilegeCallTogether) 
                    {
                        if (loc1[0] != ECountryPrivilege._ECountryPrivilegeOpenCountryBuff) 
                        {
                            if (loc1[0] != ECountryPrivilege._ECountryPrivilegeOpenCountryTreasure) 
                            {
                                if (loc1[0] == ECountryPrivilege._ECountryPrivilegeStartKill) 
                                {
                                    this._privillageOper.addPrivilegeOper("startKill");
                                }
                            }
                            else 
                            {
                                this._privillageOper.addPrivilegeOper("countryTreasure");
                            }
                        }
                        else 
                        {
                            this._privillageOper.addPrivilegeOper("countryBuff");
                        }
                    }
                    else 
                    {
                        this._privillageOper.addPrivilegeOper("callTogether");
                    }
                }
            }
            else 
            {
                loc2 = 1;
                while (loc2 < loc1.length) 
                {
                    if (loc1[loc2] == this._position) 
                    {
                        this._privillageOper.addPrivilegeOper("positionOper");
                    }
                    ++loc2;
                }
            }
            return;
        }

        public updateOfficerInfo(arg1: SOfficerInfo): void
        {
            var loc1=null;
            var loc2=null;
            this._playerName.htmlText = "";
            this._officerInfo = arg1;
            this._privillageOper.officerInfo = this._officerInfo;
            this._kingSex = Cache.instance.country.kingSex;
            if (arg1) 
            {
                loc1 = arg1.player;
                this._playerName.htmlText = loc1.name;
            }
            else 
            {
                this._playerName.htmlText = "<font color=\'#9f9f9f\'>" + Language.getString(36438) + "</font>";
            }
            if (this._info) 
            {
                loc2 = "";
                if (this._kingSex != ESex._ESexMan) 
                {
                    if (this._kingSex == ESex._ESexWoman) 
                    {
                        this._posName.htmlText = this._info.nameEx;
                    }
                }
                else 
                {
                    this._posName.htmlText = this._info.name;
                }
                if (this._info.position != ECountryPosition._ECountryPositionKing) 
                {
                    if (this._info.position != ECountryPosition._ECountryPositionQueen) 
                    {
                        loc2 = "countryPosition_" + this._info.position;
                    }
                    else if (this._officerInfo && this._officerInfo.player.sex == ESex._ESexWoman) 
                    {
                        loc2 = "countryPosition_" + ECountryPosition._ECountryPositionQueen;
                    }
                    else if (this._officerInfo && this._officerInfo.player.sex == ESex._ESexMan) 
                    {
                        loc2 = "countryPosition_" + ECountryPosition._ECountryPositionKing;
                    }
                    else if (this._kingSex != ESex._ESexMan) 
                    {
                        if (this._kingSex == ESex._ESexWoman) 
                        {
                            loc2 = "countryPosition_" + ECountryPosition._ECountryPositionKing;
                        }
                    }
                    else 
                    {
                        loc2 = "countryPosition_" + ECountryPosition._ECountryPositionQueen;
                    }
                }
                else if (this._kingSex != ESex._ESexMan) 
                {
                    if (this._kingSex == ESex._ESexWoman) 
                    {
                        loc2 = "countryPosition_" + ECountryPosition._ECountryPositionQueen;
                    }
                }
                else 
                {
                    loc2 = "countryPosition_" + ECountryPosition._ECountryPositionKing;
                }
                this._modelPlayer.load(loc2 + ".swf", ModelType.NPC, null);
                LoaderHelp.addResCallBack(ResFileConst.countryPosition, this.onCountryResLoaded);
            }
            this._privillageOper.playerName = this._playerName.text;
            this._privillageOper.posName = this._posName.text;
            this.updatePositionInfo();
            return;
        }

        /* internal  */onModelLoadComplete(arg1: SWFPlayer): void
        {
            arg1.move(20, 210);
            addChildAt(arg1, 0);
            arg1.timeRate = 18;
            arg1.updateCurrentFrame(0);
            arg1.play();
            if (arg1.movieClipData) 
            {
                arg1.movieClipData.isClear(false);
            }
            return;
        }

        /* internal  */onCountryResLoaded(): void
        {
            if (this._info) 
            {
                this._posNameBmp.bitmapData = null;
                if (this._info.position != ECountryPosition._ECountryPositionKing) 
                {
                    if (this._info.position != ECountryPosition._ECountryPositionQueen) 
                    {
                        this._posNameBmp.bitmapData = GlobalClass.getBitmapData("cPosition_" + this._info.position);
                    }
                    else if (this._kingSex == ESex._ESexMan && this._officerInfo && this._officerInfo.player.sex == ESex._ESexMan) 
                    {
                        this._posNameBmp.bitmapData = GlobalClass.getBitmapData("cPositionEx_" + ECountryPosition._ECountryPositionQueen);
                    }
                    else if (this._kingSex == ESex._ESexWoman && this._officerInfo && this._officerInfo.player.sex == ESex._ESexWoman) 
                    {
                        this._posNameBmp.bitmapData = GlobalClass.getBitmapData("cPosition_" + ECountryPosition._ECountryPositionQueen);
                    }
                    else if (this._kingSex != ESex._ESexMan) 
                    {
                        this._posNameBmp.bitmapData = GlobalClass.getBitmapData("cPositionEx_" + this._info.position);
                    }
                    else 
                    {
                        this._posNameBmp.bitmapData = GlobalClass.getBitmapData("cPosition_" + this._info.position);
                    }
                }
                else if (this._kingSex != ESex._ESexMan) 
                {
                    this._posNameBmp.bitmapData = GlobalClass.getBitmapData("cPositionEx_" + this._info.position);
                }
                else 
                {
                    this._posNameBmp.bitmapData = GlobalClass.getBitmapData("cPosition_" + this._info.position);
                }
                this._playerNameBg.bitmapData = GlobalClass.getBitmapData(ImagesConst.cPositionItemBg);
                this.resetNamePos();
            }
            return;
        }

        /* internal  */resetNamePos(): void
        {
            if (this._info == null) 
            {
                return;
            }
            if (this._info.position != ECountryPosition._ECountryPositionMarquis1) 
            {
                if (this._info.position != ECountryPosition._ECountryPositionMarquis2) 
                {
                    if (this._info.position != ECountryPosition._ECountryPositionMinister) 
                    {
                        if (this._info.position == ECountryPosition._ECountryPositionChancellor || this._info.position == ECountryPosition._ECountryPositionGeneral) 
                        {
                            this._posNameBmp.x = -10;
                        }
                    }
                    else 
                    {
                        this._posNameBmp.y = 15;
                        this._playerNameBg.y = 40;
                        this._playerName.y = 40;
                    }
                }
                else 
                {
                    this._posNameBmp.x = -20;
                    this._posNameBmp.y = 15;
                    this._playerNameBg.y = 40;
                    this._playerName.y = 40;
                }
            }
            else 
            {
                this._posNameBmp.x = -20;
            }
            return;
        }

        /* internal */ /* var */_posName: GTextFiled;

        /* internal */ /* var */_playerName: GTextFiled;

        /* internal */ /* var */_posNameBmp: Object /* flash.display.Bitmap */;

        /* internal */ /* var */_playerNameBg: Object /* flash.display.Bitmap */;

        /* internal */ /* var */_modelPlayer: SWFPlayer;

        /* internal */ /* var */_privillageOper: CountryPivillageOperPanel;

        /* internal */ /* var */_operBtn: GLoadedButton;

        /* internal */ /* var */_position: int;

        /* internal */ /* var */_selfCountryInfo: SPlayerCountryInfo;

        /* internal */ /* var */_selfPosition: int=2;

        /* internal */ /* var */_info: TCountryPrivilege;

        /* internal */ /* var */_officerInfo: SOfficerInfo;

        /* internal */ /* var */_kingSex: int;

        /* internal */ /* var */_oper: string="";

        /* internal */ /* var */_privilegeArr: Array<any>;

        /* internal */ /* var */_btnX: int=-40;

        /* internal */ /* var */_btnY: int=200;
    }
